package org.example.library.controller;

import org.example.library.pojo.User;
import org.example.library.pojo.dto.ResponseMessage;
import org.example.library.pojo.dto.UserDto;
import org.example.library.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService userService;
    //增
    @PostMapping
    public ResponseMessage<User> add(@Validated @RequestBody UserDto user) {
        User userNew = userService.add(user);
        return ResponseMessage.success(userNew);

    }
    //查
    @GetMapping("/{userId}")
    public ResponseMessage<User> get(@PathVariable Integer userId) {
        User userNew = userService.getUser(userId);
        return ResponseMessage.success(userNew);
    }
    //改
    @PutMapping
    public ResponseMessage<User> edit(@Validated @RequestBody UserDto user) {
        User userNew = userService.edit(user);
        return ResponseMessage.success(userNew);

    }

    //删
    @DeleteMapping("/{userId}")
    public ResponseMessage<User> delete(@PathVariable Integer userId) {
        User userNew = userService.delete(userId);
        return ResponseMessage.success(userNew);
    }
}
